﻿using System;
using System.Windows;
using DCT.Model;

namespace DCT.ViewModel
{
    public enum TrackSegmentViewModelType
    {
        Start,
        Turn,
        End
    }

    public class TrackSegmentViewModel : DeviceBaseViewModel
    {
        #region Constructor

        public TrackSegmentViewModel(TrackSegmentModel tsModel, TrackSegmentGraphModel tsGraphModel, TrackSegmentViewModelType tsType)
            : base(tsModel, tsGraphModel, new Point(0.5, 0.5))
        {
            if (tsModel == null)
                throw new ArgumentNullException("TrackSegmentModel");

            if (tsGraphModel == null)
                throw new ArgumentNullException("TrackSegmentGraphModel");

            _tsModel = tsModel;
            _tsGraphModel = tsGraphModel;
            _tsViewModelType = tsType;
        }

        #endregion // Constructor

        #region Fields

        TrackSegmentModel _tsModel;
        TrackSegmentGraphModel _tsGraphModel;
        TrackSegmentViewModelType _tsViewModelType;
        TrackSegmentViewModel _anotherTSViewModel;

        #endregion // Fields

        #region Delegates

        public delegate void UpdateVisualDelegate();
        public UpdateVisualDelegate UpdateVisual;

        #endregion //Delegates

        #region Properties

        public TrackSegmentModel TrackSegmentModel
        {
            get
            {
                return _tsModel;
            }
        }

        public TrackSegmentGraphModel TrackSegmentGraphModel
        {
            get
            {
                return _tsGraphModel;
            }
        }

        public TrackSegmentViewModel AnotherTSViewModel
        {
            get
            {
                return _anotherTSViewModel;
            }
        }

        public TrackSegmentViewModelType TSViewModelType
        {
            get
            {
                return _tsViewModelType;
            }
        }

        public override Point PositionPoint
        {
            get
            {
                try
                {
                    if (_tsViewModelType == TrackSegmentViewModelType.Start)
                    {
                        return Point.Parse(_tsGraphModel.StartPoint);
                    }
                    else
                    {
                        return Point.Parse(_tsGraphModel.EndPoint);
                    }
                    
                }
                catch
                {
                    return new Point();
                }
            }
            set
            {
                if (_tsViewModelType == TrackSegmentViewModelType.Start)
                {
                    if (value.ToString() != _tsGraphModel.StartPoint)
                    {
                        this.InsertUnDoRedoForProperty("PositionPoint", value, this.PositionPoint);

                        _tsGraphModel.StartPoint = value.ToString();
                        base.OnPropertyChanged("PositionPoint");
                        if(this.UpdateVisual != null)
                        {
                            this.UpdateVisual();
                        }
                    }
                }
                else
                {
                    if (value.ToString() != _tsGraphModel.EndPoint)
                    {
                        this.InsertUnDoRedoForProperty("PositionPoint", value, this.PositionPoint);

                        _tsGraphModel.EndPoint = value.ToString();
                        base.OnPropertyChanged("PositionPoint");
                        if (this.UpdateVisual != null)
                        {
                            this.UpdateVisual();
                        }
                    }
                } 
            }
        }

        public override double DeviceHeight
        {
            get
            {
                return base.DeviceHeight;
            }
        }

        public override double DeviceWidth
        {
            get
            {
                return base.DeviceWidth;
            }
        }

        //public override bool IsSelected
        //{
        //    get
        //    {
        //        return _isSelected;
        //    }
        //    set
        //    {
        //        if (value != _isSelected)
        //        {
        //            _isSelected = value;
        //            this.AnotherTSViewModel.IsSelected = value;
        //            base.OnPropertyChanged("IsSelected");
        //        }
        //    }
        //}

        public override bool IsDisplay
        {
            get
            {
                return this.DeviceMapViewModel.DeviceViewModelCollection.Contains(this);
            }
            set
            {
                if (value != this.IsDisplay)
                {
                    if (value)
                    {
                        this.DeviceMapViewModel.DeviceViewModelCollection.Add(this);
                        this.DeviceMapViewModel.DeviceViewModelCollection.Add(this._anotherTSViewModel);
                    }
                    else
                    {
                        this.DeviceMapViewModel.DeviceViewModelCollection.Remove(this);
                        this.DeviceMapViewModel.DeviceViewModelCollection.Remove(this._anotherTSViewModel);
                    }

                    base.OnPropertyChanged("IsDisplay");
                    this._anotherTSViewModel.OnPropertyChanged("IsDisplay");
                }
            }
        }

        #endregion //Properties

        #region Methods

        public void SetAnotherTrackSegmentViewModel(TrackSegmentViewModel tsViewModel)
        {
            if (tsViewModel == null)
            {
                throw new ArgumentNullException("AnotherTrackSegmentViewModel");
            }

            if (tsViewModel.TSViewModelType == this.TSViewModelType)
            {
                throw new ArgumentException("AnotherTrackSegmentViewModel");
            }

            this._anotherTSViewModel = tsViewModel;
        }

        #endregion // Methods
    }
}
